package com.jlc.book.shop.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionValidatorFilter implements Filter {

	public void destroy() {
		
	}

	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest hreq=(HttpServletRequest)req;
		String uri=hreq.getRequestURI();
		if(uri.endsWith("login.jlc") || uri.endsWith("register.jlc") || uri.endsWith("forgetPassword.jlc")){
			chain.doFilter(req, res);
		}else{
			HttpSession sess=hreq.getSession(false);
			System.out.println("session "+sess);
			if(sess==null){  
				System.out.println("if block");  
				req.setAttribute("loginError", "Session timeout.Please login again");
				req.getRequestDispatcher("index.jsp").forward(req, res);
			}else{
				chain.doFilter(req, res);
			}
		}
		
	}

	public void init(FilterConfig fConfig) throws ServletException {
		
	}

}
